package learn.basic;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.StringWriter;

import org.gephi.io.exporter.api.ExportController;
import org.gephi.io.exporter.preview.PDFExporter;
import org.gephi.io.exporter.spi.CharacterExporter;
import org.gephi.io.exporter.spi.Exporter;
import org.gephi.io.exporter.spi.GraphExporter;
import org.gephi.io.importer.api.Container;
import org.gephi.io.importer.api.EdgeDefault;
import org.gephi.io.importer.api.ImportController;
import org.gephi.io.processor.plugin.DefaultProcessor;
import org.gephi.project.api.ProjectController;
import org.gephi.project.api.Workspace;
import org.openide.util.Lookup;

import com.itextpdf.text.PageSize;

public class ExportGraph {

	public static void main(String[] args) {
		ProjectController pc = Lookup.getDefault().lookup(ProjectController.class);
		pc.newProject();
		Workspace workspace = pc.getCurrentWorkspace();
		
		ImportController importController = Lookup.getDefault().lookup(ImportController.class);
		
		Container container;
		try {
			File file = new File("Data/polblogs.gml");
			container = importController.importFile(file);
			container.getLoader().setEdgeDefault(EdgeDefault.DIRECTED);
			container.setAllowAutoNode(false);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			return;
		}
		
		importController.process(container, new DefaultProcessor(), workspace);
		
		ExportController ec = Lookup.getDefault().lookup(ExportController.class);
		try {
			ec.exportFile(new File("d://io_gexf.gexf"));
		} catch (IOException e) {
			e.printStackTrace();
			return;
		}
		
		GraphExporter exporter = (GraphExporter) ec.getExporter("gexf");
		exporter.setExportVisible(true);
		exporter.setWorkspace(workspace);
		try {
			ec.exportFile(new File("d://io_gexf1.gexf"));
		} catch (IOException e) {
			e.printStackTrace();
			return;
		}
		
		Exporter exporterGraphML = ec.getExporter("graphml");
		exporterGraphML.setWorkspace(workspace);
		StringWriter stringWriter = new StringWriter();
		ec.exportWriter(stringWriter, (CharacterExporter) exporterGraphML);
		
		PDFExporter pdfExporter =(PDFExporter) ec.getExporter("pdf");
		pdfExporter.setPageSize(PageSize.A0);
		pdfExporter.setWorkspace(workspace);
		try {
			ec.exportFile(new File("d://io_gexf.pdf"));
		} catch (IOException e) {
			e.printStackTrace();
			return;
		}
	}

}